package com.sc.nft.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.qiniu.storage.StreamUploader;
import com.sc.nft.admin.request.GetSmsCodeRequest;
import com.sc.nft.admin.request.GetSmsNewCodeRequest;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.annotation.WithoutSign;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoService;
import com.sc.nft.service.SmsCodeRecordService;
import com.sc.nft.service.StaticDataService;
import com.sc.nft.sup.Result;
import com.sc.nft.util.CheckAccountUtil;
import com.sc.nft.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * 服务控制器
 *
 * @author Sun
 * @since 2022-05-29 17:37:58
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/staticData")
@Api(tags ="静态资源")
public class StaticDataController {
    private final StaticDataService staticDataService;
    private final DaoService daoService;
    private final RedisUtil redisUtil;
    private final Redisson redisson;
    @Resource
    private SmsCodeRecordService smsCodeRecordService;
    @ApiOperation(value = "后台获取验证码", notes = "后台获取验证码", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getSmsOtherCode", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result getSmsOtherCode(@Valid @RequestBody GetSmsCodeRequest getSmsCodeRequest) {
        String noticePhone = staticDataService.getByType("notice_phone").getValue();
        Integer types=1;
        if (!CheckAccountUtil.checkPhone(noticePhone)){
            types=2;
        }
        HashMap<String, String> map = smsCodeRecordService.getAdminCode(noticePhone,
                getSmsCodeRequest.getType(),
                types);
        return Result.success(map);
    }





    @ApiOperation(value = "获取注册验证码(新)", notes = "获取注册验证码(新)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/sendRegisteSmsNew", produces = MediaType.APPLICATION_JSON_VALUE )
    @WithoutSign
    public Result sendRegisteSmsNew(@Valid @RequestBody GetSmsNewCodeRequest request) {
        String key = StrUtil.format("{}{}", RedisKeyEnum.MESSAGE_AUTHENTICATION_CODE.getKey(), request.getUserAccount());
        RLock lock = redisson.getLock(key);
        boolean b = lock.tryLock();
        HashMap<String, String>map=null;
        try {
            if (!b) {
                throw new GlobalRunTimeException("验证码已发送");
            }
             map = smsCodeRecordService.sendRegisteSms(request.getUserAccount(),
                    request.getType(),
                    request.getTypes());
        } finally {
            if (b) {
                lock.unlock();
            }
        }
        return Result.success(map);
    }




    private final StringEncryptor encryptor;
    @ApiOperation(value = "testJaspy", notes = "testJaspy", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/testJaspy", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result testJaspy() {
         String a = "XFz2%gPn";
        String encrypt = encryptor.encrypt(a);
        System.err.println(encrypt);
        String decrypt = encryptor.decrypt(encrypt);
        System.err.println(decrypt);
        return Result.success();
    }

}